import { queryRole, selRoleAllNoPaging, setAuth, addRole, selRoleByName, delRole } from '@/services/role';
import { errNotification, successNotification } from '@/utils/notification';


export default {

    namespace: 'role',
    
    state: {
        roleList: [],
        roleListNoPaging: [],
        page: 1,
        total: 0,
        addViewShow: false
    },
    
    effects: {
        *fetchSelRoleAll(param, { call, put }) {
            const response = yield call(queryRole, param);
            if(response.success){
                yield put({
                    type: 'selRoleAll',
                    payload: response.data,
                });
            }
        },
        *fetchSelRoleAllNoPaging(param, { call, put }) {
            const response = yield call(selRoleAllNoPaging, param);
            if(response.success){
                yield put({
                    type: 'selRoleAllNoPaging',
                    payload: response.data,
                });
            }
        },
        *fetchAddRole(param, { call, put }) {
            let action = param.type
            param.type = null
            const res1 = yield call(selRoleByName, param)
            if(res1.success && res1.data){//判重结果
                param.type = action
                const res = yield call(addRole, param);
                if(!res.success){
                    errNotification("角色添加失败","")
                }
                delete param.payload.roleName//删除角色名避免按角色名查询
                param.type = null
                successNotification("角色添加成功","")
                const response = yield call(queryRole, param);
                if(response.success){
                    yield put({
                        type: 'selRoleAll',
                        payload: response.data,
                    });
                    yield put({
                        type: 'addRoleViewShow',
                        payload: false,
                    });
                }
            }else{
                errNotification("角色名重复","")
            }
        },
        *fetchSetAuth(param, { call, put }) {
            const res = yield call(setAuth, param);
            param.type = null
            const response = yield call(queryRole, param);
            if(response.success){
                yield put({
                    type: 'selRoleAll',
                    payload: response.data,
                });
            }
        },
        *fetchDelRole(param, { call, put }) {
            const res = yield call(delRole, param);
            if(res.success){
                successNotification("角色删除成功","")
            }
            param.type = null
            const response = yield call(queryRole, param);
            if(response.success){
                yield put({
                    type: 'selRoleAll',
                    payload: response.data,
                });
            }
        },
    },
    
    reducers: {
        selRoleAllNoPaging(state, action){
            return {
                ...state,
                roleListNoPaging: action.payload,
            };
        },
        selRoleAll(state, action) {
            if(action.payload.list){
                action.payload.list.map((item,index)=>{
                    item.key = index
                })
            }
            return {
                ...state,
                roleList: action.payload.list,
                page: action.payload.pageNum,
                total: action.payload.total                
            };
        },
        addRoleViewShow(state, action) {
            return {
                ...state,
                addViewShow: action.payload.addViewShow           
            };
        },
    },

}